package com.fyzk.activity.trend;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanxs11 on 17/6/29.
 * 双色球走势图
 */

public class DLTTrendView extends TrendBaseView {

    private final int redPrizeCount = 5;
    private final int prizeCount = 7;
    private final int redCount = 35;
    private final int blueCount = 12;

    public DLTTrendView(Context context) {
        super(context);
    }

    public DLTTrendView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DLTTrendView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    boolean isShowBottom(List<Drawinfor> infors) {

        // 初始化底部小球信息
        infors.clear();
        for (int i = 0; i < (redCount+blueCount); i++) {
            Drawinfor drawinfor = new Drawinfor();

            if (i < redCount) {
                drawinfor.type = 0;
            } else {
                drawinfor.type = 1;
            }

            if ((i%redCount+1) < 10) {
                drawinfor.text = "0"+(i%redCount+1);
            } else {
                drawinfor.text = ""+(i%redCount+1);
            }
            infors.add(drawinfor);
        }

        return infors.size() != 0;
    }

    @Override
    protected void onDrawTop(Canvas canvas, Rect rect) {
        super.onDrawTop(canvas, rect);
        int itemHeight = rect.height()/3;
        paintLine.setColor(lineColor);
        Rect prizeRect = new Rect(rect.left, rect.top, rect.left+7*unitLength, rect.bottom);
        drawText(canvas, paintTop, "开奖号码", prizeRect);
        canvas.drawLine(prizeRect.right-1, prizeRect.top, prizeRect.right-1, prizeRect.bottom, paintLine);

        Rect topRect1 = new Rect(prizeRect.right,rect.top,rect.right,rect.top+itemHeight);
        drawText(canvas, paintTop, "基本走势", topRect1);
        canvas.drawLine(topRect1.left, topRect1.bottom, topRect1.right, topRect1.bottom, paintLine);

        Rect redRect = new Rect(prizeRect.right,topRect1.bottom,prizeRect.right+redCount*unitLength,topRect1.bottom+itemHeight);
        drawText(canvas, paintTop, "红球", redRect);
        canvas.drawLine(redRect.left, redRect.bottom, redRect.right, redRect.bottom, paintLine);
        canvas.drawLine(redRect.right, redRect.top, redRect.right, redRect.bottom, paintLine);

        Rect blueRect = new Rect(redRect.right,topRect1.bottom,redRect.right+blueCount*unitLength,topRect1.bottom+itemHeight);
        drawText(canvas, paintTop, "蓝球", blueRect);
        canvas.drawLine(blueRect.left, blueRect.bottom, blueRect.right, blueRect.bottom, paintLine);

        for (int i = 1; i <= redCount; i++) {
            Rect redNumRect = new Rect(redRect.left+unitLength*(i-1),redRect.bottom,redRect.left+unitLength*i,redRect.bottom+redRect.height());
            if (i < 10) {
                drawText(canvas, paintTop, "0"+i, redNumRect);
            } else {
                drawText(canvas, paintTop, ""+i, redNumRect);
            }
            if (i == redCount) {
                paintLine.setColor(lineColor);
            } else {
                paintLine.setColor(lineColor_L);
            }
            canvas.drawLine(redNumRect.right, redNumRect.top, redNumRect.right, redNumRect.bottom, paintLine);
        }

        for (int i = 1; i <= blueCount; i++) {
            Rect blueNumRect = new Rect(blueRect.left+unitLength*(i-1),blueRect.bottom,blueRect.left+unitLength*i,blueRect.bottom+blueRect.height());
            if (i < 10) {
                drawText(canvas, paintTop, "0"+i, blueNumRect);
            } else {
                drawText(canvas, paintTop, ""+i, blueNumRect);
            }
            if (i < blueCount) {
                paintLine.setColor(lineColor_L);
                canvas.drawLine(blueNumRect.right, blueNumRect.top, blueNumRect.right, blueNumRect.bottom, paintLine);
            }
        }

        paintLine.setColor(textDefault);
        canvas.drawLine(rect.left, rect.bottom-1, rect.right, rect.bottom-1, paintLine);
    }

    @Override
    protected void onDrawLeft(Canvas canvas, int left, int top) {
        super.onDrawLeft(canvas, left, top);
    }

    @Override
    protected void onDrawBottom(Canvas canvas, Rect rect) {
        super.onDrawBottom(canvas, rect);

//        int prizeNumWidth = 7*unitLength;   // 开奖号码占据的宽度
        int prizeNumWidth = 0;   // 开奖号码占据的宽度

        Rect chooseNumRect = new Rect(rect.left+prizeNumWidth,rect.top,rect.right,rect.top+rect.height()*2/3);
        Rect alreadyChooseNumRect = new Rect(rect.left+prizeNumWidth+getScrollX(),rect.top+rect.height()/2,rect.right,rect.bottom);

        Rect numRect = new Rect(alreadyChooseNumRect.left,alreadyChooseNumRect.top,alreadyChooseNumRect.left+unitLength*2/3,alreadyChooseNumRect.bottom);
        for (int i = 0; i < bottomInfors.size(); i++) {
            Drawinfor drawinfor = bottomInfors.get(i);
            Rect redNumRect = new Rect(chooseNumRect.left+unitLength*(i),chooseNumRect.top,chooseNumRect.left+unitLength*(i+1),chooseNumRect.bottom);
            drawinfor.rect = redNumRect;

            if (drawinfor.type == 0) {
                paintCircle.setColor(redColor);
            } else {
                paintCircle.setColor(blueColor);
            }

            if (drawinfor.isCheck) {
                paintText.setColor(Color.WHITE);
                paintCircle.setStyle(Paint.Style.FILL);

                drawText(canvas, paintCircle, drawinfor.text, numRect);
                numRect = new Rect(numRect.right, numRect.top, numRect.right+unitLength*2/3,numRect.bottom);
            } else {
                paintText.setColor(paintCircle.getColor());
                paintCircle.setStyle(Paint.Style.STROKE);
            }
            canvas.drawCircle(redNumRect.centerX(), redNumRect.centerY(), redNumRect.width()/2-4, paintCircle);

            drawText(canvas, paintText, drawinfor.text, redNumRect);

        }

    }

    @Override
    protected void onDrawCenter(Canvas canvas, int left, int top) {
        int startRow = 0;
        int startColumn = 0;

        List<Drawinfor> drawinfors = new ArrayList<>();

        // 中间
        for (int i = startColumn; i < columnCount; i++) {
            Rect bgRect = new Rect(left, top+i*unitLength, drawWidth, top+(i+1)*unitLength);
            if (i % 2 == 0) {
                paintBg.setColor(bgBai);
            } else {
                paintBg.setColor(bgHui);
            }

            canvas.drawRect(bgRect, paintBg);
            paintLine.setColor(textDefault);
            canvas.drawLine(bgRect.left, bgRect.bottom, bgRect.right, bgRect.bottom, paintLine);

            for (int j = startRow; j < rowCount; j++) {
                rectCenter.left = left + j*unitLength;
                rectCenter.right = rectCenter.left + unitLength;
                rectCenter.top = top + i*unitLength;
                rectCenter.bottom = rectCenter.top + unitLength;

                String text = ""+j;

                if (j < redPrizeCount) {    // 开奖号码——红球
                    paintText.setColor(redColor);
                    if (infors.size() > 0) {
                        int num = infors.get(i).get(j);
                        if (num < 10) {
                            text = "0"+num;
                        } else {
                            text = ""+num;
                        }
                    }
                } else if (j >= redPrizeCount && j < prizeCount) {    // 开奖号码——蓝球
                    paintText.setColor(blueColor);
                    paintLine.setColor(lineColor);
                    if (j == prizeCount-1) {
                        canvas.drawLine(rectCenter.right-1, rectCenter.top, rectCenter.right-1, rectCenter.bottom, paintLine);
                    }
                    if (infors.size() > 0) {
                        int num = infors.get(i).get(j);
                        if (num < 10) {
                            text = "0"+num;
                        } else {
                            text = ""+num;
                        }
                    }
                } else {    // 走势（红球（7-39）和蓝球（大于40））
                    paintText.setColor(textColorMiss);
                    if (j == prizeCount+redCount-1) {
                        paintLine.setColor(lineColor);
                        canvas.drawLine(rectCenter.right-1, rectCenter.top, rectCenter.right-1, rectCenter.bottom, paintLine);
                    } else {
                        paintLine.setColor(lineColor_L);
                        canvas.drawLine(rectCenter.right, rectCenter.top, rectCenter.right, rectCenter.bottom, paintLine);
                    }

                    if (infors.size() > 0) {
                        if (infors.get(i).get(j) == 0) {
                            int num = (j-prizeCount+1);
                            Drawinfor drawinfor = new Drawinfor();
                            if (j < prizeCount+redCount) {
                                drawinfor.type = 0;
                            } else {
                                num = j-(prizeCount+redCount-1);
                                drawinfor.type = 1;

                            }
                            if (num < 10) {
                                text = "0"+num;
                            } else {
                                text = ""+num;
                            }
                            drawinfor.text = text;
                            drawinfor.rect = new Rect(rectCenter.left, rectCenter.top, rectCenter.right, rectCenter.bottom);
                            if (isTJ && i >= columnCount-4) {
                            } else {
                                drawinfors.add(drawinfor);
                            }

                        } else {
                            if (isYL) {
                                text = ""+infors.get(i).get(j);
                            } else {
                                if (isTJ && i >= columnCount-4) {
                                    text = ""+infors.get(i).get(j);
                                } else {
                                    text = "";
                                }
                            }
                        }
                    }

                }

                // 绘制统计数据
                if (isTJ && i >= columnCount-4) {
                    if (j < prizeCount) {
                        text = "";
                    }
                    if (i == columnCount-4) {
                        paintLine.setColor(textDefault);
                        canvas.drawLine(bgRect.left, bgRect.top, bgRect.right, bgRect.top, paintLine);
                        paintText.setColor(color_showCount);
                    } else if (i == columnCount - 3) {

                        paintText.setColor(color_average);
                    } else if (i == columnCount - 2) {

                        paintText.setColor(color_maxMiss);
                    } else if (i == columnCount - 1) {

                        paintText.setColor(color_maxLian);
                    }
                }

                drawText(canvas, paintText, text, rectCenter);
            }

        }

        List<Rect> lineRects = new ArrayList<>();
        for (int i = 0; i < drawinfors.size(); i++) {
            if (drawinfors.get(i).type == 1) {
                lineRects.add(drawinfors.get(i).rect);
            }
        }

//        // 画蓝球折线
//        for (int i = 0; i < lineRects.size(); i++) {
//            if (i > 0) {
//                Rect rectLine1 = lineRects.get(i-1);
//                Rect rectLine2 = lineRects.get(i);
//                canvas.drawLine(rectLine1.centerX(), rectLine1.centerY(), rectLine2.centerX(), rectLine2.centerY(), paintBlueLine);
//            }
//        }

        for (int i = 0; i < drawinfors.size(); i++) {
            paintText.setColor(bgBai);
            if (drawinfors.get(i).type == 0) {
                paintCircle.setColor(redColor);
            } else {
                paintCircle.setColor(blueColor);
            }
            Rect rectCircle = drawinfors.get(i).rect;
            paintCircle.setStyle(Paint.Style.FILL);
            canvas.drawCircle(rectCircle.centerX(), rectCircle.centerY(), rectCircle.width()/2-4, paintCircle);
            drawText(canvas, paintText, drawinfors.get(i).text, rectCircle);
        }
    }

}
